﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TTQT;
using TTQT.TTQT_Code;
using TTQT.TTQT_Code.Info;

public partial class DanhMuc_Reports : MasterControl //System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblError.Text = string.Empty;
        if (!Page.IsPostBack)
        {
            bindGrid();
            visiblePanels(true, false);
            return;
        }
    }
    private void bindGrid()
    {
        try
        {
           Report r=new Report();
            datagrid.DataSource= r.SelectAll(null).Tables[0];
            datagrid.DataBind();
        }
        catch (Exception)
        {
        }
    }
    private void visiblePanels(bool visibleGrid, bool visibleEdit)
    {
        this.pnGroupGrid.Visible = visibleGrid;
        this.pnEdit.Visible = visibleEdit;
    }
    protected void cmdAddNew_Click(object sender, EventArgs e)
    {
        lblError.Text = "";
        lblUpdateStatus.Text = "";
        visiblePanels(false, true);
        txtReportId.Text = string.Empty;
        txtReportId.Enabled = true;
        txtTenBaoCao.Text = string.Empty;
        txtTenFile.Text = string.Empty;
        
        SEP_CN.Checked = false;
        KSV_QC_CN.Checked = false;
        KSV_QC_PGD.Checked = false;
        GDV_PGD.Checked = false;
        
        //TuNgay.Checked = false;
        //DenNgay.Checked = false;
        //TinhChatAC.Checked = false;
        //DotPhatHanh.Checked = false;
        //LoaiAnChi.Checked = false;
        //KhoDen.Checked = false;
        //PGD.Checked = false;
        //GDV.Checked = false;
        
        
        
        lblMode.Text = "them";
    }

    protected void cmdUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            // Nếu là sửa Thì ID sẽ khác rỗng
            string sql = "";
            if (lblMode.Text.Equals("sua"))
            {
                Report r=new Report();
                r.ReportID = txtReportId.Text;
                r.ReportName = txtTenBaoCao.Text;
                r.TenFile = txtTenFile.Text;
                //r.TinhChatAC = TinhChatAC.Checked ? true : false;
                //r.DotPhatHanh = DotPhatHanh.Checked ? true : false;
                //r.LoaiAnChi = LoaiAnChi.Checked ? true : false;
                //r.TuNgay = TuNgay.Checked ? true : false;

                //r.DenNgay = DenNgay.Checked ? true : false;
                //r.KhoDen = KhoDen.Checked ? true : false;
                //r.CN = CN.Checked ? true : false;

                //r.PGD = PGD.Checked ? true : false;
                //r.GDV = GDV.Checked ? true : false;

                r.SEP_CN = SEP_CN.Checked ? true : false;
                r.KSV_QC_CN = KSV_QC_CN.Checked ? true : false;

                r.KSV_QC_PGD = KSV_QC_PGD.Checked ? true : false;
                r.GDV_PGD = GDV_PGD.Checked ? true : false;
                r.Update(null);
                bindGrid();
                visiblePanels(true, false);
            }
            // Nếu là thêm mới
            else
            {
                Report r = new Report();
                r.ReportID = txtReportId.Text;
                r.ReportName = txtTenBaoCao.Text;
                r.TenFile = txtTenFile.Text;
                //r.TinhChatAC = TinhChatAC.Checked ? true : false;
                //r.DotPhatHanh = DotPhatHanh.Checked ? true : false;
                //r.LoaiAnChi = LoaiAnChi.Checked ? true : false;
                //r.TuNgay = TuNgay.Checked ? true : false;

                //r.DenNgay = DenNgay.Checked ? true : false;
                //r.KhoDen = KhoDen.Checked ? true : false;
                //r.CN = CN.Checked ? true : false;
                //r.PGD = PGD.Checked ? true : false;
                //r.GDV = GDV.Checked ? true : false;

                r.SEP_CN = SEP_CN.Checked ? true : false;
                r.KSV_QC_CN = KSV_QC_CN.Checked ? true : false;

                r.KSV_QC_PGD = KSV_QC_PGD.Checked ? true : false;
                r.GDV_PGD = GDV_PGD.Checked ? true : false;
                r.Insert(null);
                bindGrid();
                visiblePanels(true, false);
            }
        }
        catch (Exception ex)
        {
            lblUpdateStatus.Attributes.Add("style", "color:red;font-weight:bold;");
            this.lblUpdateStatus.Text = ex.ToString();
            //Log methods here
        }
    }
    protected void cmdCancel_Click(object sender, EventArgs e)
    {
        lblError.Text = "";
        lblUpdateStatus.Text = "";
        visiblePanels(true, false);
    }
    protected void datagrid_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        if (e.CommandName == "cmdEdit")
        {
            txtReportId.Enabled = false;
            lblMode.Text = "sua";
            visiblePanels(false, true);
            Report r= new Report();
            r.ReportID = e.Item.Cells[1].Text;
            r.selectByID(null, r.ReportID);

            txtReportId.Text = r.ReportID;
            txtTenBaoCao.Text =r.ReportName;
            txtTenFile.Text = r.TenFile;
            
            SEP_CN.Checked = r.SEP_CN;
            KSV_QC_CN.Checked = r.KSV_QC_CN;
            KSV_QC_PGD.Checked = r.KSV_QC_PGD;
            GDV_PGD.Checked = r.GDV_PGD;
            //DenNgay.Checked = r.DenNgay;
            //TuNgay.Checked = r.TuNgay;
            //TinhChatAC.Checked = r.TinhChatAC;
            //DotPhatHanh.Checked = r.DotPhatHanh;
            //LoaiAnChi.Checked = r.LoaiAnChi;
            //KhoDen.Checked = r.KhoDen;
            //CN.Checked = r.CN;
            //PGD.Checked = r.PGD;
            //GDV.Checked = r.GDV;
            // Save old info
        }
        else if (e.CommandName == "cmdDelete")
        {
            Report r = new Report();
            r.ReportID = e.Item.Cells[1].Text;
            r.Delete(null);
            bindGrid();
        }
    }
    protected void datagrid_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex < 0) return;
        Button cmdDel = (Button)e.Item.FindControl("cmdDelete");
        if (cmdDel != null) cmdDel.Attributes.Add("onclick", "delete_Confirm");
    }
    protected void datagrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        datagrid.CurrentPageIndex = e.NewPageIndex;
        bindGrid();
    }
}
